﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace BaiDuSitemap.Tools
{
    public class Gather
    {
        /// <summary>
        /// 通过url对应获取title、快照时间、快照地址等
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <returns>SearchResult</returns>
        public static string getDate(string url)
        {
            string rval = "";
            string uri = DBO.Setting.Get("URI");
            string regex = DBO.Setting.Get("Regex");
            string DefaultPages = DBO.Setting.Get("DefaultPage");

            string HtmlData = getHtml(string.Format(uri, url), "", Encoding.UTF8).Replace(" ", "").Replace("\n", "").Replace("\r", "");
            Regex myRegex = new Regex(regex, RegexOptions.IgnoreCase);
            MatchCollection myMatchCollection = myRegex.Matches(HtmlData);
            if (myMatchCollection.Count > 0)
            {
                foreach (Match myMatch in myMatchCollection)
                {
                    Regex cpRegex = new Regex(("^(http://)?{url}(/)?(" + DefaultPages + ")?$").Replace(".", @"\.").Replace("{url}", url), RegexOptions.IgnoreCase);
                    bool b = false;

                    //第一次检查网址，直接使用采集到的URL地址
                    b = cpRegex.IsMatch(myMatch.Groups["url"].Value);
                    //第二次检查网址，使用搜索引擎302跳转页面中地址
                    if (!b) b = getURL(myMatch.Groups["url"].Value, "", Encoding.UTF8, new Regex(("(http://)?{url}(/)?(" + DefaultPages + ")?").Replace(".", @"\.").Replace("{url}", url), RegexOptions.IgnoreCase));
                    //第三次检查网址，使用采集到的URL进行访问得到最终页面地址进行判断
                    if (!b) b = cpRegex.IsMatch(getURL(myMatch.Groups["url"].Value, string.Format(uri, url)));

                    if (b)
                    {
                        rval = myMatch.Groups["date"].Value;
                        break;
                    }
                }
            }

            return rval;
        }

        /// <summary>
        /// 获取网页源代码
        /// </summary>
        /// <param name="url">要访问的网址</param>
        /// <param name="ref_url">来源网址</param>
        /// <param name="encode">编码</param>
        /// <returns>string</returns>
        public static string getHtml(string url, string ref_url, Encoding encode)
        {
            string outHtmlData = "";
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse webResponse = null;
            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url.ToLower().StartsWith("http://") ? url : ("http://" + url));
                httpWebRequest.Referer = ref_url;
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727,; .NET CLR 3.0.04506,; .NET CLR 3.5.21022)";
                httpWebRequest.Method = "GET";
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encode);
                    outHtmlData = sr.ReadToEnd();
                    sr.Close();
                }
                else
                {
                    outHtmlData = "<title>" + webResponse.StatusCode + "</title>";
                }
                webResponse.Close();
            }
            catch (WebException ex)
            {
                return "<title>" + ((HttpWebResponse)ex.Response).StatusCode + "</title>";
            }
            catch
            {
                return "";
            }
            return outHtmlData;
        }

        /// <summary>
        /// 获取302跳转页面的URL
        /// </summary>
        /// <param name="url">要访问的网址</param>
        /// <param name="ref_url">来源网址</param>
        /// <param name="encode">编码</param>
        /// <param name="regex">正则表达式，用来匹配302跳转的网址</param>
        /// <returns>bool</returns>
        public static bool getURL(string url, string ref_url, Encoding encode, Regex regex)
        {
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse webResponse = null;
            bool b = false;
            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url.ToLower().StartsWith("http://") ? url : ("http://" + url));
                httpWebRequest.AllowAutoRedirect = false;
                httpWebRequest.Referer = ref_url;
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727,; .NET CLR 3.0.04506,; .NET CLR 3.5.21022)";
                httpWebRequest.Method = "GET";
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encode);
                b = regex.IsMatch(sr.ReadToEnd());

                webResponse.Close();
            }
            catch
            {
            }

            return b;
        }

        /// <summary>
        /// 获取最终访问网页的地址
        /// </summary>
        /// <param name="url">初始访问的页面地址</param>
        /// <param name="ref_url">来源地址</param>
        /// <returns>string</returns>
        public static string getURL(string url,string ref_url)
        {
            string outURL = "";
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse webResponse = null;
            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url.ToLower().StartsWith("http://") ? url : ("http://" + url));
                httpWebRequest.Referer = ref_url;
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727,; .NET CLR 3.0.04506,; .NET CLR 3.5.21022)";
                httpWebRequest.Method = "GET";
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                outURL = webResponse.ResponseUri.ToString();
            }
            catch
            {
            }
            finally
            {
                webResponse.Close();
            }
            return outURL;
        }

        /// <summary>
        /// 获取访问网页的返回状态
        /// </summary>
        /// <param name="url">初始访问的页面地址</param>
        /// <returns>HttpStatusCode</returns>
        public static HttpStatusCode getStatusCode(string url)
        {
            HttpStatusCode status = HttpStatusCode.NotFound;
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse webResponse = null;
            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url.ToLower().StartsWith("http://") ? url : ("http://" + url));
                httpWebRequest.Referer = "";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727; HT.co.Ltd)";
                httpWebRequest.Method = "GET";
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                status = webResponse.StatusCode;
            }
            catch (WebException ex)
            {
                return ((HttpWebResponse)ex.Response).StatusCode;
            }
            finally
            {
                if (webResponse != null) webResponse.Close();
            }
            return status;
        }
    }
}
